package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class RoadTestCheckBCSM extends BaseActivity {

  private static final String TAG = RoadTestCheckBCSM.class.getSimpleName();

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.bcsm_143)
  BCSMCheckItemView bcsm143;

  @BindView(R.id.bcsm_144)
  BCSMCheckItemView bcsm144;

  @BindView(R.id.bcsm_145)
  BCSMCheckItemView bcsm145;

  @BindView(R.id.bcsm_146)
  BCSMCheckItemView bcsm146;

  @BindView(R.id.bcsm_147)
  BCSMCheckItemView bcsm147;

  @BindView(R.id.bcsm_148)
  BCSMCheckItemView bcsm148;

  @BindView(R.id.bcsm_149)
  BCSMCheckItemView bcsm149;

  @BindView(R.id.bcsm_150)
  BCSMCheckItemView bcsm150;

  @BindView(R.id.bcsm_151)
  BCSMCheckItemView bcsm151;

  @BindView(R.id.bcsm_152)
  BCSMCheckItemView bcsm152;

  @BindView(R.id.bcsm_153)
  BCSMCheckItemView bcsm153;

  @BindView(R.id.bcsm_154)
  BCSMCheckItemView bcsm154;

  @BindView(R.id.activity_road_test_check_bcsm)
  LinearLayout activityRoadTestCheckBcsm;

  private Map<Integer, BCSMCheckItemView> itemViewList = null;

  private List<EvaluateDetailResult2.DetectionBean.RacingMarkBean> racingMarkList = null;

  @Override
  protected void init() {
    setContentView(R.layout.er_activity_road_test_check_bcsm);
    ButterKnife.bind(this);

    itemViewList = new HashMap<>();
    itemViewList.put(143, bcsm143);
    itemViewList.put(144, bcsm144);
    itemViewList.put(145, bcsm145);
    itemViewList.put(146, bcsm146);
    itemViewList.put(147, bcsm147);
    itemViewList.put(148, bcsm148);
    itemViewList.put(149, bcsm149);
    itemViewList.put(150, bcsm150);
    itemViewList.put(152, bcsm151);
    itemViewList.put(153, bcsm153);
    itemViewList.put(154, bcsm154);
  }

  @Override
  protected void initView() {

    vTopbar.setLeftBack();
    vTopbar.setRight("保存", -1, this);
    vTopbar.setTitle("路试检查补充说明");

    if (null != MyApplication.getInstance().getEvaluateResult().getDetection()
        && null
            != MyApplication.getInstance().getEvaluateResult().getDetection().getRacing_mark()) {

      racingMarkList =
          MyApplication.getInstance().getEvaluateResult().getDetection().getRacing_mark();

      if (racingMarkList.size() > 0) {
        displayInfo();
      }
    }
  }

  private void displayInfo() {

    for (EvaluateDetailResult2.DetectionBean.RacingMarkBean bean : racingMarkList) {

      itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus() - 1);
      itemViewList.get(bean.getId()).setTag(bean);
    }
  }

  @Override
  protected void initData() {}

  @Override
  public void onClick(View v) {
    super.onClick(v);

    if (racingMarkList == null) {
      racingMarkList = new ArrayList<>();
    } else {
      racingMarkList.clear();
    }

    if (v.getId() == R.id.vRight) {

      for (int index : itemViewList.keySet()) {
        if (3 != itemViewList.get(index).getCurrentCheckedIndex()) {

          racingMarkList.add(
              new EvaluateDetailResult2.DetectionBean.RacingMarkBean(
                  index, itemViewList.get(index).getCurrentCheckedIndex() + 1));
        }
      }

      String myData =
          new StringBuilder("{\"racing_mark\":")
              .append(new Gson().toJson(racingMarkList))
              .append("}")
              .toString();

      Log.e(TAG, "onClick: " + new Gson().toJson(racingMarkList));

      HashMap<String, String> params = new HashMap<>();
      //            params.put("authkey",
      // UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
      //            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(),
      // ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
      params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());

      params.put("detection", myData);

      ApiClient.postForm(
          HttpConstants.INSERT_ARCHIVE_INFO,
          params,
          new RespCallback<EvaluateDetailResult2>() {

            @Override
            protected void onRequestFailed(String msg) {
              SToast.show(msg);
            }

            @Override
            protected void onSuccess(EvaluateDetailResult2 data) {
              if (null != data) {
                MyApplication.getInstance().setEvaluateResult(data);
                SToast.show("保存成功");
                MyApplication.getInstance().removeActivity(RoadTestCheckBCSM.class);
                finish();
              }
            }

            @Override
            public void onBefore(Request request, int id) {
              showDialog();
            }

            @Override
            public void onAfter(int id) {
              dismissDialog();
            }
          });
    }
  }
}
